<!DOCTYPE html>
<html lang="zh-Hans">
<!--浏览器搞笑标题-->
<script type="text/javascript" src="/js/FunnyTitle.js"></script>
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=PT+Mono:300,300italic,400,400italic,700,700italic%7CMa+Shan+Zheng:300,300italic,400,400italic,700,700italic%7CKiwi+Maru:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css">
  <script src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"></script>

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"ec1ipse.gitee.io","root":"/","images":"/images","scheme":"Gemini","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta property="og:type" content="article">
<meta property="og:title" content="Go! Rust!">
<meta property="og:url" content="https://ec1ipse.gitee.io/2021/05/12/blog012-go-rust/index.html">
<meta property="og:site_name" content="eclipse&#39;s blog">
<meta property="og:locale">
<meta property="og:image" content="https://gitee.com/ec1ipse/pics-storage/raw/master/img/20210512175236.png">
<meta property="og:image" content="https://gitee.com/ec1ipse/pics-storage/raw/master/img/20210512180831.png">
<meta property="og:image" content="https://gitee.com/ec1ipse/pics-storage/raw/master/img/20210512180942.png">
<meta property="og:image" content="https://gitee.com/ec1ipse/pics-storage/raw/master/img/20210512181016.png">
<meta property="article:published_time" content="2021-05-12T09:47:46.000Z">
<meta property="article:modified_time" content="2021-05-13T18:20:35.000Z">
<meta property="article:author" content="eclipse">
<meta property="article:tag" content="Rust">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/ec1ipse/pics-storage/raw/master/img/20210512175236.png">


<link rel="canonical" href="https://ec1ipse.gitee.io/2021/05/12/blog012-go-rust/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-Hans'
  };
</script>
<title>Go! Rust! | eclipse's blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

  <!--浏览器搞笑标题-->
  <script type="text/javascript" src="/js/FunnyTitle.js"></script>
<link rel="alternate" href="/atom.xml" title="eclipse's blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">eclipse's blog</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">普通なんかごめんだ<br>僕は僕でいさせてくれ</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Installation"><span class="nav-number">1.</span> <span class="nav-text">Installation</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#What-is-Carogo"><span class="nav-number">2.</span> <span class="nav-text">What is Carogo</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Plugins-in-VSCode"><span class="nav-number">3.</span> <span class="nav-text">Plugins in VSCode</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Variables"><span class="nav-number">4.</span> <span class="nav-text">Variables</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Buit-in-Data-Types"><span class="nav-number">5.</span> <span class="nav-text">Buit-in Data Types</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Integer"><span class="nav-number">5.1.</span> <span class="nav-text">Integer</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Float"><span class="nav-number">5.2.</span> <span class="nav-text">Float</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Boolean"><span class="nav-number">5.3.</span> <span class="nav-text">Boolean</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Charater-and-String"><span class="nav-number">5.4.</span> <span class="nav-text">Charater and String</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Tuple"><span class="nav-number">5.5.</span> <span class="nav-text">Tuple</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Struct"><span class="nav-number">6.</span> <span class="nav-text">Struct</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Enum"><span class="nav-number">7.</span> <span class="nav-text">Enum</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="eclipse"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">eclipse</p>
  <div class="site-description" itemprop="description">时光和才华是相互转换的<br>我的一生就是拿时光去换取才华</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">25</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/eclipse-tenet" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;eclipse-tenet" rel="noopener" target="_blank">GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/ec1ipse" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;ec1ipse" rel="noopener" target="_blank">Gitee</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://ec1ipse.gitee.io/2021/05/12/blog012-go-rust/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="eclipse">
      <meta itemprop="description" content="时光和才华是相互转换的<br>我的一生就是拿时光去换取才华">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="eclipse's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Go! Rust!
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-05-12 17:47:46" itemprop="dateCreated datePublished" datetime="2021-05-12T17:47:46+08:00">2021-05-12</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-14 02:20:35" itemprop="dateModified" datetime="2021-05-14T02:20:35+08:00">2021-05-14</time>
      </span>

  
      </div>
      <div class="post-meta">
    <span class="post-meta-item" title="Symbols count in article">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">Symbols count in article: </span>
      <span>2.3k</span>
    </span>
    <span class="post-meta-item" title="Reading time">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">Reading time &asymp;</span>
      <span>2 mins.</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p><a target="_blank" rel="noopener" href="https://www.rust-lang.org/" title="Rust Official Website"><img src="https://gitee.com/ec1ipse/pics-storage/raw/master/img/20210512175236.png" /></a></p>
<span id="more"></span>

<h1 id="Installation"><a href="#Installation" class="headerlink" title="Installation"></a>Installation</h1><p><a target="_blank" rel="noopener" href="https://www.rust-lang.org/">Rust Programming Language (rust-lang.org)</a></p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># Install</span>
<span class="token function">curl</span> --proto <span class="token string">'=https'</span> --tlsv1.2 -sSf https://sh.rustup.rs <span class="token operator">|</span> <span class="token function">sh</span>
<span class="token comment"># Update</span>
rustup update<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<h1 id="What-is-Carogo"><a href="#What-is-Carogo" class="headerlink" title="What is Carogo"></a>What is Carogo</h1><p>Cargo is the Rust build tool and package manager.</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">cargo new      <span class="token comment"># Create a new project template</span>
cargo init     <span class="token comment"># Create a new project inside a project dir</span>
cargo build    <span class="token comment"># Build a project</span>
cargo run      <span class="token comment"># Build and run a project</span>
cargo <span class="token builtin class-name">test</span>     <span class="token comment"># Test a project</span>
cargo check    <span class="token comment"># Check project types</span>
cargo doc      <span class="token comment"># Build document for a project</span>
cargo publish  <span class="token comment"># Publish a library to crates.io</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h1 id="Plugins-in-VSCode"><a href="#Plugins-in-VSCode" class="headerlink" title="Plugins in VSCode"></a>Plugins in VSCode</h1><p><img src="https://gitee.com/ec1ipse/pics-storage/raw/master/img/20210512180831.png" alt="image-20210512180831178"></p>
<p><img src="https://gitee.com/ec1ipse/pics-storage/raw/master/img/20210512180942.png" alt="image-20210512180942754"></p>
<p><img src="https://gitee.com/ec1ipse/pics-storage/raw/master/img/20210512181016.png" alt="image-20210512181016711"></p>
<h1 id="Variables"><a href="#Variables" class="headerlink" title="Variables"></a>Variables</h1><p>Two keywords:</p>
<ul>
<li>let</li>
<li>println!</li>
</ul>
<pre class="line-numbers language-txt" data-language="txt"><code class="language-txt">Details about &#96;println!&#96;:
- We add the exclamation mark ! when we call println! to let Rust know that we&#39;re using this function as a macro and not a normal method.
- The println macro takes a string as a first argument and one extra argument for each &#123;&#125; inside that string and replaces them for the argument&#39;s display value.<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token keyword">let</span> num <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
<span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">"The number is &#123;&#125;"</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token keyword">let</span> <span class="token keyword">mut</span> num <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
num <span class="token operator">+=</span> <span class="token number">5</span><span class="token punctuation">;</span>
<span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">"The number is &#123;&#125;"</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token comment">// Shadowing</span>
<span class="token keyword">let</span> num <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> num <span class="token operator">=</span> num <span class="token operator">+</span> <span class="token number">5</span><span class="token punctuation">;</span>
<span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">"The number is &#123;&#125;"</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<h1 id="Buit-in-Data-Types"><a href="#Buit-in-Data-Types" class="headerlink" title="Buit-in Data Types"></a>Buit-in Data Types</h1><h2 id="Integer"><a href="#Integer" class="headerlink" title="Integer"></a>Integer</h2><table>
<thead>
<tr>
<th align="left">LENGTH</th>
<th align="left">SIGNED</th>
<th align="left">UNSIGNED</th>
</tr>
</thead>
<tbody><tr>
<td align="left">8-bit</td>
<td align="left"><code>i8</code></td>
<td align="left"><code>u8</code></td>
</tr>
<tr>
<td align="left">16-bit</td>
<td align="left"><code>i16</code></td>
<td align="left"><code>u16</code></td>
</tr>
<tr>
<td align="left">32-bit</td>
<td align="left"><code>i32</code></td>
<td align="left"><code>u32</code></td>
</tr>
<tr>
<td align="left">64-bit</td>
<td align="left"><code>i64</code></td>
<td align="left"><code>u64</code></td>
</tr>
<tr>
<td align="left">128-bit</td>
<td align="left"><code>i128</code></td>
<td align="left"><code>u128</code></td>
</tr>
<tr>
<td align="left"><em>architecture-dependant</em></td>
<td align="left"><code>isize</code></td>
<td align="left"><code>usize</code></td>
</tr>
</tbody></table>
<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token keyword">let</span> num <span class="token operator">=</span> <span class="token number">333i32</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> num<span class="token punctuation">:</span> <span class="token keyword">i32</span> <span class="token operator">=</span> <span class="token number">333</span><span class="token punctuation">;</span>  <span class="token comment">// 0b33 0o33 0x33</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="Float"><a href="#Float" class="headerlink" title="Float"></a>Float</h2><table>
<thead>
<tr>
<th>LENGTH</th>
<th>FORMAT</th>
</tr>
</thead>
<tbody><tr>
<td>32-bit</td>
<td><code>f32</code></td>
</tr>
<tr>
<td>64-bit</td>
<td><code>f64</code> [default]</td>
</tr>
</tbody></table>
<h2 id="Boolean"><a href="#Boolean" class="headerlink" title="Boolean"></a>Boolean</h2><h2 id="Charater-and-String"><a href="#Charater-and-String" class="headerlink" title="Charater and String"></a>Charater and String</h2><pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token comment">// Characters are wrapped by ''</span>
<span class="token keyword">let</span> c <span class="token operator">=</span> <span class="token char string">'z'</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> z <span class="token operator">=</span> <span class="token char string">'ℤ'</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> heart_eyed_cat <span class="token operator">=</span> '😻'<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token comment">// Generally speacking, common understanding of strings</span>
<span class="token comment">// refer to String and &amp;str.</span>
<span class="token comment">// String is an abstract of 'Vec&lt;u8>'</span>
<span class="token keyword">let</span> hello <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">::</span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string">"Hello,"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> world <span class="token operator">=</span> <span class="token string">" world!"</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Tuple"><a href="#Tuple" class="headerlink" title="Tuple"></a>Tuple</h2><pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token keyword">let</span> rectangle<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token keyword">i32</span><span class="token punctuation">,</span> <span class="token keyword">i32</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">"The width: &#123;&#125;; the height: &#123;&#125;"</span><span class="token punctuation">,</span> rectangle<span class="token number">.0</span><span class="token punctuation">,</span> rectangle<span class="token number">.1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h1 id="Struct"><a href="#Struct" class="headerlink" title="Struct"></a>Struct</h1><pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token keyword">struct</span> <span class="token type-definition class-name">Person</span> <span class="token punctuation">&#123;</span>
  	name<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">,</span>
  	age<span class="token punctuation">:</span> <span class="token keyword">i32</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">let</span> a_person <span class="token operator">=</span> <span class="token class-name">Person</span> <span class="token punctuation">&#123;</span>
  	name<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">::</span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string">"Eclipse"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  	age<span class="token punctuation">:</span> <span class="token number">20</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

<span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">"&#123;&#125;'s age is &#123;&#125;.'"</span><span class="token punctuation">,</span> a_person<span class="token punctuation">.</span>name<span class="token punctuation">,</span> a_person<span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token comment">// Tuple struct</span>
<span class="token keyword">struct</span> <span class="token type-definition class-name">Color</span><span class="token punctuation">(</span><span class="token keyword">i8</span><span class="token punctuation">,</span> <span class="token keyword">i8</span><span class="token punctuation">,</span> <span class="token keyword">i8</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h1 id="Enum"><a href="#Enum" class="headerlink" title="Enum"></a>Enum</h1><pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token keyword">enum</span> <span class="token type-definition class-name">IpAddrKind</span> <span class="token punctuation">&#123;</span>
  	<span class="token constant">V4</span><span class="token punctuation">,</span>
  	<span class="token constant">V6</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token class-name">IpAddrKind</span><span class="token punctuation">::</span><span class="token constant">V4</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> s <span class="token operator">=</span> <span class="token class-name">IpAddrKind</span><span class="token punctuation">::</span><span class="token constant">V6</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token keyword">enum</span> <span class="token type-definition class-name">IpAddrKind</span> <span class="token punctuation">&#123;</span>
  	<span class="token constant">V4</span><span class="token punctuation">(</span><span class="token keyword">u8</span><span class="token punctuation">,</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> <span class="token keyword">u8</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  	<span class="token constant">V6</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token comment">// Option Enum</span>
<span class="token keyword">enum</span> <span class="token type-definition class-name">Option</span><span class="token operator">&lt;</span><span class="token class-name">T</span><span class="token operator">></span> <span class="token punctuation">&#123;</span>
  	<span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token class-name">T</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  	<span class="token class-name">None</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token keyword">let</span> some_number <span class="token operator">=</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> some_string <span class="token operator">=</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token string">"String"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> absent_number<span class="token punctuation">:</span> <span class="token class-name">Option</span><span class="token operator">&lt;</span><span class="token keyword">i32</span><span class="token operator">></span> <span class="token operator">=</span> <span class="token class-name">None</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token keyword">match</span> coin <span class="token punctuation">&#123;</span>
    <span class="token class-name">Coin</span><span class="token punctuation">::</span><span class="token class-name">Penny</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span>
        <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">"Lucky penny!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token number">1</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token class-name">Coin</span><span class="token punctuation">::</span><span class="token class-name">Nickel</span> <span class="token operator">=></span> <span class="token number">5</span><span class="token punctuation">,</span>
    <span class="token class-name">Coin</span><span class="token punctuation">::</span><span class="token class-name">Dime</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">,</span>
    <span class="token class-name">Coin</span><span class="token punctuation">::</span><span class="token class-name">Quarter</span> <span class="token operator">=></span> <span class="token number">25</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-rust" data-language="rust"><code class="language-rust"><span class="token comment">// Match Option&lt;T></span>
<span class="token keyword">match</span> x <span class="token punctuation">&#123;</span>
  	<span class="token class-name">None</span> <span class="token operator">=></span> <span class="token class-name">None</span><span class="token punctuation">,</span>
  	<span class="token class-name">Some</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name">Some</span><span class="token punctuation">(</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">// Only mactch one pattern</span>
<span class="token keyword">if</span> <span class="token keyword">let</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">=</span> some_u8_value <span class="token punctuation">&#123;</span>
  	<span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">"Three!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>Post author:  </strong>eclipse
  </li>
  <li class="post-copyright-link">
      <strong>Post link: </strong>
      <a href="https://ec1ipse.gitee.io/2021/05/12/blog012-go-rust/" title="Go! Rust!">https://ec1ipse.gitee.io/2021/05/12/blog012-go-rust/</a>
  </li>
  <li class="post-copyright-license">
    <strong>Copyright Notice:  </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-Hans" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> unless stating additionally.
  </li>
</ul>
</div>

          <div class="followme">
  <span>Welcome to my other publishing channels</span>

  <div class="social-list">

      <div class="social-item">
        <a target="_blank" class="social-link" href="/atom.xml">
          <span class="icon">
            <i class="fa fa-rss"></i>
          </span>

          <span class="label">RSS</span>
        </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/Rust/" rel="tag"><i class="fa fa-tag"></i> Rust</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/05/12/blog011-python-read-big-file/" rel="prev" title="使用Python读取比内存大小更大的文件">
                  <i class="fa fa-chevron-left"></i> 使用Python读取比内存大小更大的文件
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/05/13/blog013-common-network-ports/" rel="next" title="常用网络端口">
                  常用网络端口 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2021 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">eclipse</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="Symbols count total">29k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="Reading time total">30 mins.</span>
  </span>
</div>

    </div>
  </footer>

  
  <script size="300" alpha="0.6" zIndex="-1" src="https://cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>





  <script>
    NProgress.configure({
      showSpinner: true
    });
    NProgress.start();
    document.addEventListener('readystatechange', () => {
      if (document.readyState === 'interactive') {
        NProgress.inc(0.8);
      }
      if (document.readyState === 'complete') {
        NProgress.done();
      }
    });
    document.addEventListener('pjax:send', () => {
      NProgress.start();
    });
    document.addEventListener('pjax:success', () => {
      NProgress.done();
    });
  </script>

  





<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"left","width":150,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
